
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;


public class DataCleaner {
	private static final String COMMA = ",";
	private static final String parents = "usual, pretentious, great_pret ";
	private static final String has_nurs = "proper, less_proper, improper, critical, very_crit ";
	private static final String form = "complete, completed, incomplete, foster ";
	private static final String children = "1, 2, 3, more";
	private static final String housing = "convenient, less_conv, critical ";
	private static final String finance = "convenient, inconv";
	private static final String social = "nonprob, slightly_prob, problematic ";
	private static final String health = "recommended, priority, not_recom ";
	private static final String classification = "not_recom, recommend, very_recom, priority, spec_prior ";


	private	List<NurseryEntity> entities = new ArrayList<NurseryEntity>();
	private List<NurseryEntity> incompleteEntities = new ArrayList<NurseryEntity>();
	private NurseryEntity entity;
	
	private List<NurseryDTO> dtos = new ArrayList<NurseryDTO>();
	
	public List<NurseryDTO> getNurseryDTOS() {
		return dtos;
	}

	private NurseryEntityToDtoTransformer transformer = new NurseryEntityToDtoTransformer();

	public DataCleaner(String filenameIn, String filenameOut){
		readFile(filenameIn);
		writeFile(filenameOut);
	}

	private void readFile(String filename){
		BufferedReader in;
		try {
			in = new BufferedReader(new FileReader(filename));
			String line = "";

			while((line = in.readLine()) != null){
				String [] data = line.split(COMMA);

				entity = new NurseryEntity();
				for(int i = 0; i < data.length; i++){
					validateAttribute(data[i], i);
				}
				//System.out.println(dto.toString());
				if(data.length == 9 || !entity.isValid()){
					entities.add(entity);
				} else {
					incompleteEntities.add(entity);
				}
			} 
			
			System.out.println("CompleteData: " + entities.size());
			System.out.println("InCompleteData: " + incompleteEntities.size());
			in.close();

		} catch(FileNotFoundException e){
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void writeFile(String filename){
		FileWriter fw;
		try {
			System.out.println("File Creation may take a moment. " +
					"Do not open the file until file writing is complete.");
			fw = new FileWriter(filename);
			PrintWriter pw = new PrintWriter(fw);
			dtos.addAll(transformer.transform(entities));
			for(NurseryDTO dto : dtos){
				pw.write(dto.toString());
			}
			System.out.println("File Created.");
			pw.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private boolean validateAttribute(String attribute, int index) {
		switch(index){
		case 0: 
			if(parents.contains(attribute)){
				entity.setParents(attribute);
				return true;
			}; 
		case 1: 
			if (has_nurs.contains(attribute)){
				entity.setHas_nurs(attribute);
				return true;
			};
		case 2: 
			if (form.contains(attribute)){
				entity.setForm(attribute);
				return true;
			} ;
		case 3: 
			if (children.contains(attribute)){
				entity.setChildren(attribute);
				return true;
			} ;
		case 4: 
			if (housing.contains(attribute)){
				entity.setHousing(attribute);
				return true;
			};
		case 5: 
			if (finance.contains(attribute)){
				entity.setFinance(attribute);
				return true;
			};
		case 6: 
			if (social.contains(attribute)){
				entity.setSocial(attribute);
				return true;
			};
		case 7: 
			if (health.contains(attribute)){
				entity.setHealth(attribute);
				return true;
			};
		case 8: 
			if (classification.contains(attribute)){
				entity.setClassification(attribute);
				return true;
			};
		default: return false;
		}
	} 
}
